//
//  RecordViewCtrl.m
//  HuoBao
//
//  Created by 于会海 on 15/12/3.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "RecordViewCtrl.h"
#import "HBBondListCtrl.h"
#import "HBLoginManager.h"
#import "HBBuyingRecordCtrl.h"
#import "HBWithdrawalsRecordCtrl.h"
#import "HBGetUserAccountMoneyInfo.h"
#import "HBGetUserAccontMoneyParam.h"
#import "HBGetUserAccountMoneyParser.h"

@interface RecordViewCtrl ()<RecordViewPagerDataSource, RecordViewPagerDelegate>
{
    int          m;
    UIView     * topView;
    NSArray    * topLibArr;
    NSUInteger   topIndex;
    UIView     * topAlertView;
    UILabel    * topAlertLabel;
    UIButton   * topAlertBtn;
    NSString   * topStatus;
    UILabel    * topTotalInvest;
//    NSString   * accountMoney;
    NSString   * myAccountMoney;
}
@end

@implementation RecordViewCtrl

- (void)viewDidLoad {
    self.title = @"交易记录";
    topLibArr = @[@"买入记录",@"提现记录"];
    self.dataSource = self;
    self.delegate = self;
 
    [self requestTotalMoney];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    [self.view setBackgroundColor:[LCCommon getColor:@"E1E1E1"]];
    [super viewDidLoad];
}

- (void)requestTotalMoney{
    HBGetUserAccontMoneyParam* param = [[HBGetUserAccontMoneyParam alloc] init];
    HBGetUserAccountMoneyParser* parser = [[HBGetUserAccountMoneyParser alloc] init];
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    
    [NetCenter getDataFromURL:Method_getUserAccountMoney params:param parser:parser result:^(ParserResult *myResult) {
        HBGetUserAccountMoneyInfo* info = (HBGetUserAccountMoneyInfo*)myResult.result;
        if (myResult.code == 0) {
            [self createTopUIWithAccountMoney:info.accountMoney];
            myAccountMoney = info.accountMoney;
        }
        else if(myResult.code == 1){
            DISSMISS_ERR(myResult.message);
        }
    } failure:^(NSError *error) {
        DISSMISS_ERR(error.description);
    }];
    
}

- (void)createTopUIWithAccountMoney:(NSString* )accountMoney{
    topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 105)];
    [topView setBackgroundColor:[LCCommon getColor:Main_BackGround_Color]];
    [self.view addSubview:topView];

    CGFloat HEIGHT;
    if ([accountMoney floatValue] == 0 || accountMoney == NULL) {
        topTotalInvest = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2 - 60, topView.size.height/2 -10, 120, 3)];
        [topTotalInvest setBackgroundColor:[LCCommon getColor:@"FA9838"]];
        HEIGHT = 20;
        [topView addSubview:topTotalInvest];
    }else{
        topTotalInvest = [[UILabel alloc] initWithFrame: CGRectMake(0, topView.size.height/2 -30, SCREEN_WIDTH, 50)];
        topTotalInvest.backgroundColor = [UIColor clearColor];
        topTotalInvest.textColor = [LCCommon getColor:@"FA9838"];
        topTotalInvest.textAlignment = NSTextAlignmentCenter;
        topTotalInvest.text = accountMoney;
        topTotalInvest.font = [UIFont systemFontOfSize:36];
        HEIGHT = 0.0;
        [topView addSubview:topTotalInvest];
    }
    
    UILabel* botLabel = [self LabelWithRect:CGRectMake(0, topTotalInvest.origin.y+topTotalInvest.size.height + HEIGHT, SCREEN_WIDTH, 14) textColor:[LCCommon getColor:@"929292"] textstr:@"在投金额(元)" textfont:12 textstyle:NSTextAlignmentCenter];
    [topView addSubview:botLabel];
    
    UIButton* topButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [topButton setFrame:CGRectMake(0, 0, 200, 105)];
    topButton.centerX = topView.centerX;
    [self.view addSubview:topButton];
    [topButton addTarget:self action:@selector(topClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)topClicked:(UIButton*) button{
    LLog(@"进入债权列表 %@",myAccountMoney);
    if (myAccountMoney&&myAccountMoney.length>0&&[myAccountMoney floatValue]!=0)
    {
        HBBondListCtrl *bondListCtrl = [[HBBondListCtrl alloc]init];
        bondListCtrl.totalMoney = myAccountMoney;
        [self.navigationController pushViewController:bondListCtrl animated:YES];
    }
    
}

-(UILabel *)LabelWithRect:(CGRect)rect textColor:(UIColor *)color textstr:(NSString *)str textfont:(int)font textstyle:(NSTextAlignment)aligment{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.textColor = color;
    label.text = str;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = aligment;
    label.numberOfLines = 0;
    return label;
}

- (void)setTopAlertViewStatusWithString:(NSString*)status{
    topStatus = @"1";
    NSDictionary* dic = [NSDictionary dictionaryWithObject:topStatus forKey:@"myindex"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"setNewFream" object:dic];
}

- (void)deleteTopAlert:(UIButton*)button{
    UIButton* bigButton = button;
    if (bigButton.selected == YES) {
        [topAlertBtn setSelected:YES];
    }
    [UIView animateWithDuration:0.35 animations:^{
        CGRect myFrame=self.view.frame;
        myFrame.origin.y=20.0;
        self.view.frame=myFrame;
    }];
    topStatus = @"0";
    NSDictionary* dic = [NSDictionary dictionaryWithObject:topStatus forKey:@"myindex"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"setNewFream" object:dic];  
}

- (void)viewWillAppear:(BOOL)animated{
    //这里接收过来的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showTopPoint:) name:Notification_showTopPoint object:nil];
    //如果导航栏是透明的话,滚动视图要想让内部视图不上下溢出，需要设置下面的属性为NO
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = NO;
//    [self showTopAlertView:YES];
}
- (void)buildLayout{
    
}
-(void)showTopPoint:(NSNotification*) notification{
    int n = [[[notification object] valueForKey:@"recordIndex"] intValue];
    topIndex = n;
    //设置顶部标签颜色
    UILabel* label = (UILabel*)[self.view viewWithTag:n+10];
    if (m ) {
        if (m-10 != n) {
            UILabel* label1 = (UILabel*)[self.view viewWithTag:m];
            [label1 setTextColor:[LCCommon getColor:@"929292"]];
        }
    }
    if (n == label.tag - 10) {
        [label setTextColor:[UIColor whiteColor]];
        m = n+10;
    }
}

#pragma mark - ViewPagerDataSource
- (NSUInteger)numberOfTabsForViewPager:(RecordViewPagerCtrl *)viewPager {
    return topLibArr.count;
}
- (UIView *)viewPager:(RecordViewPagerCtrl *)viewPager viewForTabAtIndex:(NSUInteger)index {
    
    UILabel *label = [UILabel new];
    [label setFrame:CGRectMake(0, -10, SCREEN_WIDTH/topLibArr.count, 44)];
//    label.backgroundColor = [UIColor redColor];
    label.font = [UIFont systemFontOfSize:17];
    label.text = topLibArr[index];
    label.tag = index + 10;
    
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [LCCommon getColor:@"929292"];
    [label sizeToFit];
    if (index == 0) {
        label.textColor = [UIColor whiteColor];
        m = 0+10;
    }
    LLog(@"%@",topLibArr[index]);
    return label;
}
//在这里添加需要写入的viewController
- (UIViewController *)viewPager:(RecordViewPagerCtrl *)viewPager contentViewControllerForTabAtIndex:(NSUInteger)index {
    UIViewController* VC = [[UIViewController alloc] init];
    HBBuyingRecordCtrl* homeVC= [[HBBuyingRecordCtrl alloc] init];
    HBWithdrawalsRecordCtrl* actVC = [[HBWithdrawalsRecordCtrl alloc] init];
    if (index == 0) {
        VC = homeVC;
        homeVC.nav = self.navigationController;
    }else if(index == 1){
        VC = actVC;
        actVC.nav = self.navigationController;
    }
    return VC;
}

#pragma mark - ViewPagerDelegate
- (CGFloat)viewPager:(RecordViewPagerCtrl *)viewPager valueForOption:(RecordViewPagerOption)option withDefault:(CGFloat)value {
    switch (option) {
        case RecordViewPagerOptionTabWidth:
            return [[UIScreen mainScreen] bounds].size.width/topLibArr.count;
            break;
        default:
            break;
    }
    
    return value;
}

- (UIColor *)viewPager:(RecordViewPagerCtrl *)viewPager colorForComponent:(RecordViewPagerComponent)component withDefault:(UIColor *)color {
    
    switch (component) {
        case RecordViewPagerIndicator:
            return [[LCCommon getColor:@"fa9838"] colorWithAlphaComponent:0.64];
            break;
        default:
            break;
    }
    
    return color;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
